/**
 * 支持IE控件的示例
 * 共通部分不需要修改，仅修改注释“调用控件接口的部分”之后的内容
 * 须在注释“调用控件接口的部分”之后加入对控件接口的转发调用
 */
function AtlCtrlForET99(clsid) {
  // 共通部分不要修改
  this.ready_func_ = null;
  this.callbacks_ = new HashTable();
  this.module_ = "JS_IActiveXCtrl";
  if ("WebSocket" in window) {
    this.ws = new WebSocket("ws://127.0.0.1:7321");
  } else {
    throw "WebSocket not supported";
  }
  this.ws.onerror = function () {
    throw "Unable to establish connection to WebSocket";
  };

  this.loadModule = function () {
    var msg = JSON.stringify({
      MsgId: "LoadModule",
      Module: this.module_,
    });
    this.ws.onmessage = this._callback.bind(this);
    this.ws.send(msg);
  };
  this.loadModuleCallBack = function (result, response) {
    if (!result) throw result.Response;
    if (this.ready_func_) this.ready_func_();
  }.bind(this);

  this.callbacks_.add("LoadModule", this.loadModuleCallBack);

  this.exec = function (func, param, callback) {
    var msg_id = Object.id(callback).toString();
    this.callbacks_.add(msg_id, callback);
    var param_ = {
      MsgId: msg_id,
      Method: func + "|" + clsid,
    };
    if (param) {
      param_["Param"] = JSON.stringify(param);
    }
    var msg = JSON.stringify(param_);
    this.ws.send(msg);
  };

  this.ready = function (func) {
    this.ready_func_ = func;
  };

  this._callback = function (response) {
    var r = JSON.parse(response.data);
    var msg_id = r.MsgId;
    if (r.Result) {
      console.info(r.Response);
    } else {
      console.error(r.response);
    }
    if (!this.callbacks_.containsKey(msg_id)) {
      return;
    }
    console.log(this.callbacks_.getKeys());
    console.log(msg_id);
    var callback = this.callbacks_.getValue(msg_id);
    callback(r.Result, r.Response);
    this.callbacks_.remove(msg_id);
  };

  this.ws.onopen = this.loadModule.bind(this);

  /**
   * 调用控件接口的部分
   * 与test.html的调用名称相对应，以下均为示例
   * exec第一个参数是 ActiveX控件 的接口名称，第二个参数是ActiveX控件的 参数 传入，第三个参数不需要修改
   * 对每一个ActiveX控件接口都要写一个通过WebSocket调用的函数
   */

  this.ET99_FindToken = function (callback) {
    this.exec("ET99_FindToken", [pid], callback);
  };
  this.ET99_OpenToken = function (callback) {
    this.exec("ET99_OpenToken", [pid, parseInt(et99index)], callback);
  };
  this.ET99_GetSN = function (callback) {
    this.exec("ET99_GetSN", null, callback);
  };
  this.ET99_Verify = function (callback) {
    this.exec("ET99_Verify", [usertype, userpin], callback);
  };
  this.ET99_GenRandom = function (callback) {
    this.exec("ET99_GenRandom", null, callback);
  };
  this.ET99_SetKey = function (callback) {
    this.exec("ET99_SetKey", [parseInt(keyid), keybuff], callback);
  };
  this.ET99_HMAC_MD5 = function (callback) {
    this.exec(
      "ET99_HMAC_MD5",
      [parseInt(keyid), md5input.length, md5input],
      callback
    );
  };
  this.ET99_CloseToken = function (callback) {
    this.exec("ET99_CloseToken", null, callback);
  };
  this.ET99_Read = function (callback) {
    this.exec("ET99_Read", [parseInt(roffset), parseInt(rlen)], callback);
  };
  this.ET99_Write = function (callback) {
    this.exec(
      "ET99_Write",
      [parseInt(woffset), parseInt(wlen), databuf],
      callback
    );
  };
  this.GetMacAddress = function (callback) {
    this.exec("GetMacAddress", null, callback);
  };
  this.ET99_ChangeUserPIN = function (callback) {
    this.exec("ET99_ChangeUserPIN", [oldpin, newpin], callback);
  };
  this.ET99_GenPID = function (callback) {
    this.exec("ET99_GenPID", [pidseed], callback);
  };
}
